#! /usr/bin/env python
# encoding: utf-8

import os
import ibexutils
from waflib import Utils, Logs

def options (opt):
	# get the list of all possible interval library
	list_of_interval_lib_plugin = ibexutils.get_dirlist (opt.path)

	# set default interval library
	deflib = "gaol" if not Utils.is_win32 else "filib"
	if list_of_interval_lib_plugin == []: # this will raise a error at configure
		default_interval_lib = None
	elif deflib in list_of_interval_lib_plugin:
		default_interval_lib = deflib
	elif any(lib.startswith(deflib) for lib in list_of_interval_lib_plugin):
		L = [ lib for lib in list_of_interval_lib_plugin if lib.startswith(deflib) ]
		L.sort(key=LooseVersion)
		default_interval_lib = L[-1] # choose the latest version
	else: # use the first of the list as default
		default_interval_lib = list_of_interval_lib_plugin[0]

	# help string for --interval-lib command line option
	help_string = "Possible values: " + ", ".join(list_of_interval_lib_plugin)
	help_string += " [default: " + str(default_interval_lib) + "]"

	# add the option --interval-lib
	opt.add_option ("--interval-lib", action="store", dest="INTERVAL_LIB",
									choices = list_of_interval_lib_plugin,
									default = default_interval_lib, help = help_string)

	opt.recurse (list_of_interval_lib_plugin)

def configure (conf):
	if conf.options.INTERVAL_LIB is None:
		conf.fatal ("No interval library is available.")

	Logs.pprint ("BLUE", "Configuration of the library for interval arithmetic")
	conf.msg ("Library for interval arithmetic", conf.options.INTERVAL_LIB)

	# Recurse on the interval library directory.
	conf.recurse (conf.options.INTERVAL_LIB)

	# Copy in _IBEX_DEPS some important variables from _ITV_LIB
	# The plugin must use the store ITV_LIB (uselib_store argument with
	# conf.check* functions).
	conf.env.append_unique ("CXXFLAGS_IBEX_DEPS", conf.env.CXXFLAGS_ITV_LIB)
	if conf.env.ENABLE_SHARED:
		# if shared lib is used, 3rd party libs are compiled as static lib with
		# -fPIC and are contained in libibex
		for lib in conf.env.LIB_ITV_LIB:
			if not lib in conf.env.LIB_3RD_LIST:
				conf.env.append_unique ("LIB_IBEX_DEPS", lib)
	else:
		conf.env.append_unique ("LIB_IBEX_DEPS", conf.env.LIB_ITV_LIB)
	# Add the path of directory into include paths of ibex
	itvlib_abspath = os.path.join (conf.path.abspath(), conf.options.INTERVAL_LIB)
	conf.env.append_unique ("INCLUDES_IBEX", itvlib_abspath)
	# Need to add ibex_IntervalLibWrapper.(cpp|inl|h) to the source of ibex
	wrapper_cpp = os.path.join (itvlib_abspath, "ibex_IntervalLibWrapper.cpp")
	wrapper_h = os.path.join (itvlib_abspath, "ibex_IntervalLibWrapper.h")
	wrapper_inl = os.path.join (itvlib_abspath, "ibex_IntervalLibWrapper.inl")
	conf.env.append_unique ("IBEX_SRC", os.path.relpath (wrapper_cpp, "src"))
	conf.env.append_unique ("IBEX_HDR", os.path.relpath (wrapper_h, "src"))
	conf.env.append_unique ("IBEX_HDR", os.path.relpath (wrapper_inl, "src"))

	# The variable "INTERVAL_LIB" must be defined in env by the plugin called to
	# handle the library for interval arithmetic.
	if not conf.env["INTERVAL_LIB"]:
		conf.fatal ("INTERVAL_LIB must be defined in env by the plugin " + conf.options.INTERVAL_LIB)
	if isinstance (conf.env.IBEX_INTERVAL_LIB_INCLUDES, list):
		l = [ "#include \"%s\"" % s for s in conf.env.IBEX_INTERVAL_LIB_INCLUDES ]
		conf.env.IBEX_INTERVAL_LIB_INCLUDES  = os.linesep.join (l)

	# Add info on the interval library used to the settings
	conf.setting_define("INTERVAL_LIB", conf.env["INTERVAL_LIB"])

